﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace LunchPad.Service
{
    


    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        private static LunchpadEntities entities = new LunchpadEntities();

        public Restaurant[] getRestaurants()
        {
            ArrayList al = new ArrayList();

            try
            {
                var rest = (from r in entities.RESTAURANTs select r);
                foreach (RESTAURANT r in rest)
                {
                    al.Add(new Restaurant(r));
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return (Restaurant[])al.ToArray(typeof(Restaurant));
        }

        public int sendOptIn(int userId, double lat2, double long2, int vote)
        {
            int success = 0;

            try
            {
                var user = (from u in entities.USERs 
                            where u.USER_ID == userId
                            select u).First();

                user.CURRENT_LAT = lat2;
                user.CURRENT_LONG = long2;
                user.IS_ATTENDING = (byte)vote;

                entities.SaveChanges();

                success = 1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return success;
        }
        public double[] getOptIn(int userId)
        {
            double[] loc = { 0, 0 };

            try
            {
                var user = (from u in entities.USERs
                            where u.USER_ID == userId
                            select u).First();

                loc[0] = user.CURRENT_LAT.GetValueOrDefault(user.DEFAULT_LAT.GetValueOrDefault(0));
                loc[1] = user.CURRENT_LONG.GetValueOrDefault(user.DEFAULT_LONG.GetValueOrDefault(0));

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return loc;
        }

        public int sendRating(int userId, int restaurantId, int rating)
        {
            int success = 0;

            try
            {
                var preference = (from up in entities.USER_PREFERENCES
                                  where up.USER_ID == userId && up.RESTAURANT_ID == restaurantId
                                  select up).First();

                preference.RANK = rating;

                entities.SaveChanges();

                success = 1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return success;
        }
        public int getRating(int userId, int restaurantId)
        {
            int rank = 0;

            try
            {
                rank = (from up in entities.USER_PREFERENCES
                                  where up.USER_ID == userId && up.RESTAURANT_ID == restaurantId
                                  select up.RANK).First().GetValueOrDefault(0);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return rank;
        }

        public int sendCrave(int userId, int restaurantId, int crave)
        {
            int success = 0;

            try
            {
                var preference = (from up in entities.USER_PREFERENCES
                                  where up.USER_ID == userId && up.RESTAURANT_ID == restaurantId
                                  select up).First();

                preference.CRAVE = (byte)crave;

                entities.SaveChanges();

                success = 1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return success;
        }
        public int getCrave(int userId, int restaurantId)
        {
            int crave = 0;

            try
            {
                crave = (int)(from up in entities.USER_PREFERENCES
                        where up.USER_ID == userId && up.RESTAURANT_ID == restaurantId
                        select up.CRAVE).First().GetValueOrDefault(0);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return crave;
        }

        public int sendHate(int userId, int restaurantId, int hate)
        {
            int success = 0;

            try
            {
                var preference = (from up in entities.USER_PREFERENCES
                                  where up.USER_ID == userId && up.RESTAURANT_ID == restaurantId
                                  select up).First();

                preference.HATE = (byte)hate;

                entities.SaveChanges();

                success = 1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return success;
        }
        public int getHate(int userId, int restaurantId)
        {
            int hate = 0;

            try
            {
                hate = (int)(from up in entities.USER_PREFERENCES
                              where up.USER_ID == userId && up.RESTAURANT_ID == restaurantId
                              select up.HATE).First().GetValueOrDefault(0);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return hate;
        }

        public int setSendSMS(int userId, int state)
        {
            int success = 0;

            try
            {
                var user = (from u in entities.USERs
                                  where u.USER_ID == userId
                                  select u).First();
                
                user.SMS = (byte)state;

                entities.SaveChanges();

                success = 1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return success;
        }
        public int getSendSMS(int userId)
        {
            int state = 0;

            try
            {
                state = (int)(from u in entities.USERs
                             where u.USER_ID == userId
                             select u.SMS).First();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return state;
        }

        public int setSendEmail(int userId, int state)
        {
            int success = 0;

            try
            {
                var user = (from u in entities.USERs
                            where u.USER_ID == userId
                            select u).First();

                user.EMAIL = (byte)state;

                entities.SaveChanges();

                success = 1;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return success;
        }
        public int getSendEmail(int userId)
        {
            int state = 0;

            try
            {
                state = (int)(from u in entities.USERs
                             where u.USER_ID == userId
                             select u.EMAIL).First();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return state;
        }


    }
}
